package com.app.controller.backstage;

import com.app.model.ConfigModel;
import com.app.model.HelpRecordModel;
import com.app.model.PositionModel;
import com.app.service.CategoryService;
import com.app.service.ConfigService;
import com.app.service.PositionService;
import com.app.service.impl.CategoryServiceImpl;
import com.app.util.Result;
import com.app.vo.ConfigVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Controller
public class SpecialityManageController {
    @Autowired
    private ConfigService configService;
    @Autowired
    private PositionService positionService;

    //行业列表
    //findJobName
    //添加和编辑
    @PostMapping("/insertAndUpdateSpeciality")
    public Result<Object> insertAndUpdateSpeciality(@RequestBody ConfigModel configModel) {
        if (configModel.getId() > 0) {
            configService.update(configModel);
            return Result.success("操作成功");
        } else {
            configModel.setConType(5);
            configService.insert(configModel);
            return Result.success("操作成功");
        }
    }

    //专业列表
    @GetMapping("/selectSpecialityList")
    public Result<Object> selectSpecialityList(Integer page, Integer limit, String keyword) {
        QueryWrapper<ConfigModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("con_type", 5);
        if (keyword != null && !keyword.equals("")) {
            queryWrapper.like("name", keyword);
        }
        Page<ConfigModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<ConfigModel> configModels = configService.selectPageList(pages, queryWrapper);
        for (int i = 0; i < configModels.getRecords().size(); i++) {
            StringBuilder industryText = new StringBuilder();
            if (configModels.getRecords().get(i).getIndustry() != null && !configModels.getRecords().get(i).getIndustry().equals("")) {
                String[] split = configModels.getRecords().get(i).getIndustry().split(",");
                for (int i1 = 0; i1 < split.length; i1++) {
                    PositionModel model = positionService.getModelById(Integer.parseInt(split[i1]));
                    if (model != null && i1 != split.length - 1) {
                        industryText.append(model.getJobTitle()).append(",");
                    } else if (model != null) {
                        industryText.append(model.getJobTitle());
                    }
                }
                configModels.getRecords().get(i).setIndustryText(industryText.toString());
            }
        }
        ConfigVo vo = new ConfigVo();
        vo.setList(configModels.getRecords());
        vo.setCount((int) configModels.getTotal());
        return Result.success(vo);
    }

    //删除
    @GetMapping("/deleteSpecialityList")
    public Result<Object> deleteSpecialityList(Integer id) {
        configService.delete(id);
        return Result.success("操作成功");
    }

}
